package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.ChannelDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.mapper.WmChannelMapper;
import com.heima.media.service.IWmChannelService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * 频道信息业务层实现类
 *
 * @author WuQiang
 * @date 2022/09/28
 */
@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements IWmChannelService {

    @Override
    public ResponseResult page(ChannelDto dto) {

        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //根据名称分页查询
        LambdaQueryWrapper<WmChannel> wrappers = new LambdaQueryWrapper();
        String name = dto.getName();
        wrappers.like(StringUtils.isNotBlank(name), WmChannel::getName, name)
                //根据ord升序排列
                .orderByAsc(WmChannel::getOrd);
        Page<WmChannel> channelPage = this.page(new Page<>(dto.getPage(), dto.getSize()), wrappers);
        return new PageResponseResult(
                dto.getPage(),
                dto.getSize(),
                channelPage.getTotal(),
                channelPage.getRecords());
    }

    @Override
    public ResponseResult saveWmChannel(WmChannel wmChannel) {
        //参数非空校验
        String name = wmChannel.getName();
        if (wmChannel == null || StringUtils.isBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 1、先根据名称查询数据库
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmChannel::getName, name);
        WmChannel wmChannelDB = this.getOne(wrapper);
        if (wmChannelDB != null) {
            // 1.1、该名称存在则新增失败
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        } else {
            // 1.2、不存在则执行插入操作
            wmChannel.setCreatedTime(new Date());
            this.save(wmChannel);
        }
        return ResponseResult.okResult();
    }

    /**
     * 更新频道
     *
     * @param wmChannel 频道实体
     * @return {@code ResponseResult}
     */
    @Override
    public ResponseResult updateWmChannel(WmChannel wmChannel) {
        //1、参数校验
        String name = wmChannel.getName();
        if (wmChannel == null || StringUtils.isBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2、根据频道名称查询数据库
        LambdaQueryWrapper<WmChannel> wrappers = new LambdaQueryWrapper<>();
        wrappers.eq(WmChannel::getName, wmChannel.getName());
        WmChannel wmChannelDB = this.getOne(wrappers);
        if (wmChannelDB != null) {
            //排除掉自己的情况
            if (!wmChannelDB.getId().equals(wmChannel.getId())) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
        }

        //修改频道管理
        this.updateById(wmChannel);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //1、参数校验
        if (ObjectUtils.isEmpty(id)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2、删除业务
        this.removeById(id);
        return ResponseResult.okResult();
    }
}
